package com.metadata_system.common.db.mapper;

import com.metadata_system.common.db.entity.MetadataEdge;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import tk.mybatis.mapper.common.Mapper;

import java.util.List;
import java.util.Map;

public interface MetadataEdgeMapper extends Mapper<MetadataEdge> {
    @Select("select last_insert_id()")
    int getLastInsertId();


    @Select("delete from md_metadata_edge where metadata_id=#{id}")
    void deleteByMetadataId(@Param("id") Integer id);

    @Select("select id from md_metadata_edge where metadata_id=#{id}")
    List<Integer> getIdsByMetadataId(@Param("id") Integer id);

    @Select("delete from md_metadata_edge where metadata_view_id=#{id}")
    void deleteByViewId(@Param("id") Integer id);

    @Select("delete from md_metadata_edge where source=#{id} or target=#{id}")
    void deleteByNodeId(@Param("id") Integer id);

    @Select("select id from md_metadata_edge where source=#{id} or target=#{id}")
    List<Integer> getIdsByNodeId(@Param("id") Integer id);

    @Select("select id from md_metadata_edge where metadata_view_id=#{id}")
    List<Integer> getIdsByViewId(@Param("id") Integer id);

    /**
     * 删除多个连线
     * @param idList
     * @param metadataViewId
     */
    void deleteByIdList(@Param("idList") List<Integer> idList, @Param("metadataViewId") Integer metadataViewId);

    /**
     * 根据类型获取唯一标识
     * @param type
     * @param metadataId
     * @return
     */
    @Select("select unique_key,properties from md_metadata_edge where type=#{type} and metadata_id=#{metadataId} limit 1")
    Map<String,String> getUniqueKeyByType(Integer type, Integer metadataId);

    /**
     * 根据节点ID列表获取连线ID列表
     * @param idList
     * @return
     */
    @Select("select id from md_metadata_edge where source in (${idList}) or target in (${idList})")
    List<Integer> getIdsByNodeIds(@Param("idList") String idList);
}